package com.dragon.backstage.domain.param.menu;

import com.dragon.common.utils.AjaxResult;
import com.dragon.interfaces.constant.MenuConstant;
import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 菜单模块-添加菜单请求参数
 *
 * @author dragon
 * @version 1.0
 * @date 2024/8/6 17:54
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(title = "菜单模块-添加菜单请求参数")
public class MenuAddParam {

    /**
     * 父菜单ID
     */
    @Builder.Default
    @Schema(title = "父菜单ID", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long parentId = 0L;

    /**
     * 菜单名称
     */
    @Schema(title = "菜单名称", requiredMode = Schema.RequiredMode.REQUIRED)
    private String menuName;

    /**
     * 显示顺序
     */
    @Schema(title = "显示顺序", requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer menuSort;

    /**
     * 菜单类型（C菜单 F按钮）
     */
    @Schema(title = "菜单类型（M目录 C菜单 F按钮）", requiredMode = Schema.RequiredMode.REQUIRED)
    private String menuType;

    /**
     * 显示状态（1显示 0隐藏）
     */
    @Builder.Default
    @Schema(title = "显示状态（1显示 0隐藏）", requiredMode = Schema.RequiredMode.REQUIRED)
    private Byte visible = 1;

    /**
     * 菜单状态（1显示 0隐藏）
     */
    @Builder.Default
    @Schema(title = "菜单状态（1显示 0隐藏）", requiredMode = Schema.RequiredMode.REQUIRED)
    private Byte status = 1;

    /**
     * 权限标识
     */
    @Schema(title = "权限标识")
    private String permission;

    /**
     * 备注
     */
    @Schema(title = "备注")
    private String remark;

    /**
     * 路由名称
     */
    @Schema(title = "路由名称", requiredMode = Schema.RequiredMode.REQUIRED)
    private String name;

    /**
     * 路由地址
     */
    @Schema(title = "路由地址", requiredMode = Schema.RequiredMode.REQUIRED)
    private String path;

    /**
     * 组件路径
     */
    @Schema(title = "组件路径", requiredMode = Schema.RequiredMode.REQUIRED)
    private String component;

    /**
     * 菜单图标
     */
    @Schema(title = "菜单图标")
    private String icon;

    /**
     * 菜单是否全屏
     */
    @Builder.Default
    @Schema(title = "菜单是否全屏")
    private Byte isFull = 0;

    /**
     * 活动菜单路由地址(通常用作详情页高亮父级菜单)
     */
    @Schema(title = "活动菜单路由地址(通常用作详情页高亮父级菜单)")
    private String activeMenu;

    /**
     * 路由重定向地址
     */
    @Schema(title = "路由重定向地址")
    private String redirect;

    /**
     * 路由外链时填写的访问地址
     */
    @Schema(title = "路由外链时填写的访问地址")
    private String isLink;


    /**
     * 参数校验
     *
     * @param param 参数
     * @return AjaxResult<Void>
     */
    public static AjaxResult<Void> check(MenuAddParam param) {
        // 菜单类型枚举值
        List<String> menuTypeArr = Lists.newArrayList(
            MenuConstant.MENU_TYPE_DIRECTORY,
            MenuConstant.MENU_TYPE_MENU,
            MenuConstant.MENU_TYPE_BUTTON
        );
        // 是否显示枚举值
        List<Byte> visibleArr = Lists.newArrayList(
            MenuConstant.VISIBLE_DISPLAY,
            MenuConstant.VISIBLE_HIDDEN
        );
        // 菜单状态枚举值
        List<Byte> statusArr = Lists.newArrayList(
            MenuConstant.STATUS_NORMAL,
            MenuConstant.STATUS_DISABLED
        );
        // 菜单是否全屏
        List<Byte> fullArr = Lists.newArrayList(
            MenuConstant.FULL_YES,
            MenuConstant.FULL_NO
        );

        if (Objects.isNull(param)) {
            return AjaxResult.failed("请求参数不能为空");
        }

        if (Objects.isNull(param.getParentId())) {
            return AjaxResult.failed("父菜单ID不能为空");
        }

        if (StringUtils.isBlank(param.getMenuType())) {
            return AjaxResult.failed("菜单类型不能为空");
        }

        if (!menuTypeArr.contains(param.getMenuType())) {
            return AjaxResult.failed("菜单类型不符");
        }

        if (StringUtils.isBlank(param.getMenuName())) {
            return AjaxResult.failed("菜单名称不能为空");
        }

        if (Objects.isNull(param.getMenuSort())) {
            return AjaxResult.failed("菜单显示排序不能为空");
        }

        if (Objects.isNull(param.getStatus())) {
            return AjaxResult.failed("菜单状态不能为空");
        }

        if (!statusArr.contains(param.getStatus())) {
            return AjaxResult.failed("菜单状态的值不符");
        }

        if (MenuConstant.MENU_TYPE_DIRECTORY.equals(param.getMenuType())) {
            if (Objects.isNull(param.getVisible())) {
                return AjaxResult.failed("显示状态不能为空");
            }

            if (!visibleArr.contains(param.getVisible())) {
                return AjaxResult.failed("显示状态的值不符");
            }
        }

        if (MenuConstant.MENU_TYPE_MENU.equals(param.getMenuType())) {
            if (StringUtils.isBlank(param.getName())) {
                return AjaxResult.failed("路由名称不能为空");
            }

            if (StringUtils.isBlank(param.getPath())) {
                return AjaxResult.failed("路由地址不能为空");
            }

            if (Objects.isNull(param.getVisible())) {
                return AjaxResult.failed("显示状态不能为空");
            }

            if (!visibleArr.contains(param.getVisible())) {
                return AjaxResult.failed("显示状态的值不符");
            }

            if (Objects.isNull(param.getIsFull())) {
                return AjaxResult.failed("菜单是否全屏不能为空");
            }

            if (!fullArr.contains(param.getIsFull())) {
                return AjaxResult.failed("菜单是否全屏的值不符");
            }
        }

        return AjaxResult.succeed();
    }

}
